package loveATM.atm;


import loveATM.User;
import loveATM.atm.Panel.*;
import sun.reflect.misc.FieldUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * GUI版本的ATM
 */
public class ATM extends JFrame {
    public  static final String FILE_PATH  = "users.dat";
    private Image bgImg = new ImageIcon("images/background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    private AgainPwdPanel againPwdPanel = new AgainPwdPanel(this);
    private ArrayList users ;

    public int index;
    public int otherIndex;


    public ATM(){
        init();
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });
        this.setLayout(cardLayout);


        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(savePanel,"save");
        this.add(getPanel,"get");
        this.add(searchPanel,"search");
        this.add(changePwdPanel,"changePwd");
        this.add(transferPanel,"transferBtn");
        this.add(againPwdPanel,"again");

        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
    public void init(){
        users = (ArrayList)FileUtil.readObject(FILE_PATH);
        if(users == null){
            users = new ArrayList<>();
            users.add(new User("aaaaaa","111111",1000));
            users.add(new User("bbbbbb","222222",2000));
            users.add(new User("cccccc","333333",3000));
            users.add(new User("dddddd","444444",4000));
            users.add(new User("eeeeee","555555",5000));
            FileUtil.writeObject(users,FILE_PATH);
        }
    }

    public ArrayList getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }

    public LoginPanel getLoginPanel() {
        return loginPanel;
    }

    public void showPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }
    public static void main(String[] args) {
        new ATM();
    }
}
